﻿using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Controllers
{
    public class OrderController : Controller
    {
        private readonly ILogger<OrderController> _logger;
        private readonly IRepository<User, int> _userRepostitory;
        private readonly IRepository<UserAddress, int> _userAddressRepostitory;
        private readonly IRepository<Product, int> _productRepostitory;
        private readonly IRepository<Order, int> _orderRepostitory;
        private readonly IRepository<OrderDetails, int> _orderDetailsRepostitory;

        public OrderController(ILogger<OrderController> logger, IRepository<User, int> userRepository,
             IRepository<UserAddress, int> userAddressRepostitory,
              IRepository<Product, int> productRepostitory,
               IRepository<Order, int> orderRepostitory,
                IRepository<OrderDetails, int> orderDetailsRepostitory)
        {
            _logger = logger;
            _userRepostitory = userRepository;
            _userAddressRepostitory = userAddressRepostitory;
            _productRepostitory = productRepostitory;
            _orderRepostitory = orderRepostitory;
            _orderDetailsRepostitory = orderDetailsRepostitory;

        }
        #region 订单添加

        public async Task<IActionResult> Create(int id)
        {
            var userName = HttpContext.User.Claims.First().ValueType;//获取当前登录用户的ID
            var userid = int.Parse(userName);//强类型转换
            var user = _userRepostitory.GetAllList().Where(x => x.Id == userid).FirstOrDefault();//获取当前用户的信息
            ViewBag.User = user;
            var userAddress = _userAddressRepostitory.GetAllList();
            ViewBag.Address = userAddress;
            var product = _productRepostitory.GetAllList().Where(x => x.Id == id).FirstOrDefault();//获取当前产品的信息
            ViewBag.Product = product;
            var order = _orderRepostitory.GetAllList().Last();
            return View(order);
        }
        [HttpPost]
        public async Task<IActionResult> Create(Order order)
        {
            order.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                await _orderRepostitory.InsertAsync(order);
                return Json(new { isSuccess = true, msg = "下单成功" });
            }
            return Json(new { isSuccess = false, msg = "下单失败" });
        }
        #endregion
        #region
        public async Task<IActionResult> OrderDetailCreate()
        {
            var order = _orderRepostitory.GetAllList().Last();
            return View(order);
        }
        [HttpPost]
        public async Task<IActionResult> OrderDetailCreate(OrderDetails orderDetails)
        {
            orderDetails.BuyTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                await _orderDetailsRepostitory.InsertAsync(orderDetails);
                return Json(new { isSuccess = true ,msg = "页面即将跳转，即将进入支付页面" });
            }
            return Json(new { isSuccess = true, msg = "下单失败" });
        }
        #endregion

        #region 购物车
        [HttpPost]
        public async Task<IActionResult> AddCart(Order order)
        {
            order.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                await _orderRepostitory.InsertAsync(order);
                return Json(new { isSuccess = true, msg = "已添加到购物车" });
            }
            return Json(new { isSuccess = false, msg = "添加失败" });
        }
        #endregion
    }
}
